import java.util.PriorityQueue;
import java.util.Queue;

/**
 * 703. 数据流中的第 K 大元素
 */
public class No703 {
    private final int k;
    private final Queue<Integer> heap;

    public No703(int k, int[] nums) {
        this.k = k;
        heap = new PriorityQueue<>();
        for (int num : nums) {
            add(num);
        }
    }

    public int add(int val) {
        heap.offer(val);
        if (heap.size() > k) heap.poll();
        return heap.peek();
    }
}
